//
// Created by EDZ on 2021/12/8.
//

#include <vector>
#include <cstdio>
#include <iostream>
#include <cassert>
#include "Vector.h"

Vector::Vector(int sz)
        : elem{new double[sz]}, size{sz} {
    if (sz < 0)
        std::cout << "Error" << std::endl;
    iter = &elem[0];
    first = iter;
    last = first;
    cursor = 0;
}

Vector::~Vector() {
    delete[] elem;
    iter = last = first = nullptr;
}

double &Vector::operator[](int i) {
    assert(i >= 0 && i < size);
    return elem[i];
}

const double &Vector::operator[](int i) const {
    return elem[i];
}

Vector::iterator Vector::begin() {
    return first;
}

Vector::iterator Vector::end() {
    return last;
}

Vector::iterator Vector::operator++() {
    if (iter != (elem + size - 1))
        return ++iter;
    else
        return iter;
}

Vector::iterator Vector::operator++(int n) {
    if (iter < (elem + size - 1))
        return iter++;
    else
        return iter;
}

void Vector::push_back(double e) {
    //std::printf("%p,%p\n", first, last);
    if (last != first + size) {
        *last = e;
        last += 1;
    }
    printf("%p,%p,%f\n",first, last, *last);
}

double Vector::pop_back() {
    if (last != elem) {
        last = last - 1;
    }
    return *last;
}

void func_vector_invariant(){
    Vector v{3};
    v.push_back(1.3);
    v.push_back(1.4);
    v.push_back(1.5);
    for (auto i = v.begin(); i != v.end(); ++i) {
        std::cout << "vector [n]" << *i << std::endl;
    }
    int i=0;
    for(auto x:v) {
        std::cout << "vector[" <<  i <<  "]: " << x << "; v["<< i << "]=" << v[i]<< std::endl;
        ++i;
    }
}